<template>
  <div class="equity">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <i class="el-icon-caret-bottom" />
        <span>一级权益</span>
        <el-button style="float: right; padding: 3px 0;" type="text">复制</el-button>
        <el-button
          style="float: right; padding: 3px 0;"
          type="text"
          class="btn"
          @click="dialogVisible = true"
        >编辑</el-button>
      </div>
      <div class="text item">
        <el-form
          ref="form"
          status-icon
          label-width="200px"
          class="demo-ruleForm"
          :model="form"
          :rules="rules"
        >
          <div>
            <el-form-item label="可上架商品数量：" prop="number">
              <el-input
                v-model.trim="form.number"
                type="text"
                autocomplete="off"
                placeholder="请输入"
              />
            </el-form-item>
            <el-form-item label="备注：请商家7个工作日确认" prop="remark">
              <el-input
                v-model.trim="form.remark"
                type="text"
                autocomplete="off"
                placeholder="请输入"
              />
            </el-form-item>
          </div>
          <div class="isIptRight">
            <el-form-item label="优惠价：" prop="discounts" label-width="80px">
              <el-input
                v-model.trim="form.discounts"
                type="text"
                autocomplete="off"
                placeholder="请输入"
                @blur="comparePrice"
              />
            </el-form-item>
            <el-form-item label="价格：" prop="price" label-width="80px">
              <el-input
                v-model.trim="form.price"
                type="text"
                autocomplete="off"
                placeholder="请输入"
                @blur="comparePrice"
              />
            </el-form-item>
            <el-button type="primary" size="mini" @click="isSure('form')">确定</el-button>
          </div>
        </el-form>
      </div>
    </el-card>
    <div class="add">
      +新增
    </div>
    <div>
      <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%" :before-close="compile">
        <span slot="default">
          <el-form ref="form" :rules="rules" :model="form" label-width="100px">
            <el-form-item label="上架数量：" prop="number">
              <el-input v-model="form.number" />
            </el-form-item>
            <el-form-item label="价格：" prop="price">
              <el-input v-model="form.price" />
            </el-form-item>
            <el-form-item label="优惠价：" prop="discounts">
              <el-input v-model="form.discounts" />
            </el-form-item>
            <el-form-item label="备注：" prop="remark">
              <el-input v-model="form.remark" />
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { postEquity } from '@/api/setting';

export default {
  name: 'Equity',
  data() {
    return {
      dialogVisible: false,
      form: {
        number: '',
        remark: '',
        discounts: '',
        price: '',
      },
      rules: {
        number: [
          { required: true, message: '请输入可上架的商品数量', trigger: 'blur' },
          { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' },
          { min: 1, max: 5, message: '最少上架1个，最多上架99999个', trigger: 'blur' },
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 1, max: 20, message: '长度请控制在20字以内', trigger: 'blur' },
        ],
        discounts: [
          { required: true, message: '请输入优惠价', trigger: 'blur' },
          { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' },
          { min: 1, max: 8, message: '优惠价格不能大于商品的价格', trigger: 'blur' },
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur' },
          { min: 1, max: 8, message: '价格不能超过1亿', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    comparePrice() {},
    compile(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    isSure(form) {
      if (this.form.discounts.length > 0 && this.form.price.length > 0) {
        if (Number(this.form.discounts) > Number(this.form.price)) {
          this.$message('优惠价格不能大于商品的价格');
        } else {
          this.$refs[form].validate(async(valid) => {
            if (valid) {
              const res = await postEquity(this.form);
              if (res.request.status === 201) {
                this.$message.success('提交成功！');
                this.$refs[form].resetFields();
              } else {
                this.$message.error('提交失败');
              }
            } else {
              return false;
            }
          });
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.equity {
  ::v-deep .box-card {
    height: 240px;
    margin-top: 30px;
    .clearfix {
      font-size: 14px;
      .el-icon-caret-bottom {
        margin-right: 2px;
        font-size: 16px;
      }
      .btn {
        margin-right: 15px;
      }
    }
    .el-card__header {
      background-color: #fafafa;
    }
    .text {
      .el-form {
        display: flex;
        justify-content: space-between;
        .isIptRight {
          position: relative;
          .el-input__inner {
            width: 260px;
          }
          .el-button {
            position: absolute;
            right: 0;
            bottom: -20px;
          }
        }
        .el-form-item__label {
          text-align: left;
          font-weight: 400;
        }
        .el-input__inner {
          width: 310px;
          height: 35px;
        }
      }
    }
  }
  .add {
    width: 100%;
    height: 50px;
    border: 1px dashed #27a6fd;
    border-radius: 10px;
    text-align: center;
    line-height: 50px;
    color: #27a6fd;
    background-color: #fafafa;
    font-size: 14px;
    font-weight: 700;
  }
}
</style>
